﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CrmCustomInstallActions.UI
{
    public partial class CrmSolutionInstallerForm : Form
    {
        
        /// <summary>
        /// Determines the install mode of the solution installer
        /// </summary>
        public enum InstallModes
        {
            Install = 1,
            Uninstall = 2
        }

        private Dictionary<string, object> _PropertyBag = new Dictionary<string, object>();

        public CrmSolutionInstallerForm()
        {
            InitializeComponent();
        }

        public bool Initialize(InstallModes InstallMode, string SolutionFileName, string TargetDir)
        {
            // Setup the property bag
            _PropertyBag.Add("INSTALLMODE", (int)InstallMode);
            _PropertyBag.Add("TARGETDIR", TargetDir);
            _PropertyBag.Add("SOLUTIONFILE", Path.Combine(TargetDir,SolutionFileName));
            
            // Setup the service
            UI.CrmSolutionInstallerLoginForm f = new CrmSolutionInstallerLoginForm();
            f.Authenticate();
            _PropertyBag.Add("CRMSERVICE", f.CrmService);
            _PropertyBag.Add("METADATSERVICE", f.MetadataService);
            _PropertyBag.Add("CREDENTIAL", f.Credential);
            if (f.Credential != null)
            {
                txtUserAuthentication.Text = f.Credential.Domain + "\\" + f.Credential.UserName;
            }
            f = null;

            return true;
        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void CheckPreRequisites()
        {

        }

        private void lnkChangeConfiguration_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (dlgOpen.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            if (txtConfigurationFile.Text.Length > 0 && System.IO.File.Exists(txtConfigurationFile.Text))
            {
                if (MessageBox.Show("There is already a selected configuration file. Are you sure you want to change it?", "Change Configuration File", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    return;
                }
                else
                {
                    txtConfigurationFile.Text = dlgOpen.FileName;
                    CheckPreRequisites();
                }
            }
        }

        private void lnkChangeBackupFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (dlgBrowse.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            if (txtBackupFolder.Text.Length > 0 && System.IO.Directory.Exists(txtBackupFolder.Text))
            {
                if (MessageBox.Show("There is already a selected backup folder. Are you sure you want to change it?", "Change Backup Folder", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    return;
                }
                else
                {
                    txtBackupFolder.Text = dlgBrowse.SelectedPath;
                    CheckPreRequisites();
                }

            }

        }

        private void CrmSolutionInstallerForm_Load(object sender, EventArgs e)
        {

        }

        private void lnkChangeAuthentication_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UI.CrmSolutionInstallerLoginForm form = new CrmSolutionInstallerLoginForm();
            form.ShowDialog(this);
            if (form.DialogResult == DialogResult.OK)
            {
                _PropertyBag.Add("CRMSERVICE", form.CrmService);
                _PropertyBag.Add("METADATSERVICE", form.MetadataService);
                _PropertyBag.Add("CREDENTIAL", form.Credential);
            }
        }
    }
}
